﻿package com.tdzx.net;

import org.apache.http.Header;
import org.apache.http.client.CookieStore;
import org.apache.http.message.BasicHeader;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.net.wifi.WifiManager;

public class MyHttpCookies {
	/** 每页数据显示最大数 */
	private static int pageSize = 10;
	/** 当前会话后的cookie信息 */
	private static CookieStore uCookie = null;
	/** 公用的HTTP提示头信息 */
	private static Header[] httpHeader;
	/** HTTP连接的网络节点 */
	private static String httpProxyStr;
	/**http请求的公用url部分**/
	/**上下文对象**/
	Context context;
	public static final Uri PREFERRED_APN_URI = Uri.parse("content://telephony/carriers/preferapn");
	
	public MyHttpCookies(Context context){
		this.context = context;
		/** y设置请求头 **/
		/** y设置请求头 **/
		Header[] header = {
				new BasicHeader("PagingRows", String.valueOf(pageSize)) };
		httpHeader = header;
	}
	
	/**
	 * 增加自动选择网络，自适应cmwap、CMNET、wifi或3G
	 */
	@SuppressWarnings("static-access")
	public void initHTTPProxy() {
		WifiManager wifiManager = (WifiManager) (context.getSystemService(context.WIFI_SERVICE));
		if (!wifiManager.isWifiEnabled()) {
			httpProxyStr=getApnName();
		} else {
			httpProxyStr = null;
		}
	}
	
	/**
	 * 获取APN名称
	 * 
	 * @return
	 */
	public String getApnName() {
		String apnName = "";
		try {
			Cursor cursor = context.getContentResolver()
					.query(PREFERRED_APN_URI, new String[] { "_id", "apn", "type" }, null, null, null);
			if (cursor != null) {
				cursor.moveToFirst();
				int counts = cursor.getCount();
				if (counts != 0) {
					if (!cursor.isAfterLast()) {
						apnName = cursor.getString(1);
					}
				}
				cursor.close();
			} else {
				//适配中国电信定制机,如海信EG968,上面方式获取的cursor为空，所以换种方式
				Cursor c = context.getContentResolver().query(PREFERRED_APN_URI, null, null, null, null);
				if (c != null) {
					c.moveToFirst();
					apnName = c.getString(c.getColumnIndex("user"));
					c.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			apnName = "";
		}
		return apnName;
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public CookieStore getuCookie() {
		return uCookie;
	}

	public void setuCookie(CookieStore uCookie) {
		this.uCookie = uCookie;
	}


	public Header[] getHttpHeader() {
		return httpHeader;
	}

	public String getHttpProxyStr() {
		return httpProxyStr;
	}
}
